LATEX_MK_DIR ?= $(abspath $(COMMON))
BIN_DIR = $(abspath $(COMMON))
LATEX_DIR = $(abspath $(COMMON)/latex)

PATH := $(PATH):$(BIN_DIR)
TEXINPUTS := .:$(COMMON):${LATEX_DIR}:$(TEXINPUTS)

LATEX_MK ?= latex-mk

USE_PDFLATEX = 1
RMDIR = $(RM) -rf

ifndef BUILD_FINAL
TEX_INC_SRCS += $(NAME:=.tex)
NAME := $(NAME:=-draft)
endif

CLEAN_FILES += $(NAME:=.pdfsync) $(NAME:=.cb) $(NAME:=.cb2) $(NAME:=.loc)

LATEX_ENV+= TEXINPUTS="$(TEXINPUTS)"
PDFLATEX_ENV+= TEXINPUTS="$(TEXINPUTS)"

CHECK_L2H_CFG ?= check_l2h_cfg
LATEX2HTML_CFG = $(COMMON)/latex2html.cfg
LATEX2HTML_FLAGS = -init_file $(LATEX2HTML_CFG)

TEXSRCS = $(TEX_INC_SRCS)

$(NAME)_EXTRA_DIST_FILES = \
	$(TEX_INC_SRCS) \
	README.txt
$(NAME)_EXTRA_DIST += $(NAME).pdf
ifdef BUILD_HTML
$(NAME)_EXTRA_DIST += $(NAME).html_dir
endif
$(NAME)_EXTRA_DIST += $($(NAME)_EXTRA_DIST_FILES)

ALL = pdf
ifdef BUILD_HTML
ALL += check-l2h html
else
ALL += $(NAME)_rm_html_dir
endif


.PHONY: make-default
make-default: $(ALL)

.PHONY: draft
draft:
	$(MAKE)

.PHONY: final
final:
	$(MAKE) BUILD_FINAL=1

.PHONY: clean-html
clean-html: html
	@$(RM) -f $(NAME).html_dir/WARNINGS
	@$(RMDIR) $(NAME).html_dir/TMP

$(NAME).html_dir: clean-html

.PHONY: $(NAME)_rm_html_dir
$(NAME)_rm_html_dir:
	@$(RMDIR) $(NAME).html_dir

.PHONY: check-l2h
check-l2h:
	@echo "Checking that latex2html has been installed ..."
	@which $(LATEX2HTML)
	@env LATEX2HTML='$(LATEX2HTML)' $(CHECK_L2H_CFG)

ifndef BUILD_FINAL
clean::
	$(MAKE) BUILD_FINAL=1 clean
endif

include $(LATEX_MK_DIR)/latex.gmk
